package com.zuoshenge.main.init.starter;

import com.zuoshenge.init.AbstractCache;
import com.zuoshenge.redis.util.RedisUtil;
import com.zuoshenge.redis.util.SpringContextUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

//@ConditionalOnProperty(name = {"init.cache.enable"}, havingValue = "true", matchIfMissing = true)
@Component
public class InitRun implements CommandLineRunner {


    @Override
    public void run(String... args) throws Exception {
        // 需要知道哪些缓存需要进行预热
        ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
        Map<String, AbstractCache> beanMap = applicationContext.getBeansOfType(AbstractCache.class);
        // 调用其init方法
        if (!beanMap.isEmpty()) {
            for (Map.Entry<String, AbstractCache> entry : beanMap.entrySet()) {
                // 获取AbstractCache的子类(实现缓存方法的类)，并调用其initCache()方法
                AbstractCache abstractCache = (AbstractCache) SpringContextUtil.getBean(entry.getValue().getClass());
                abstractCache.initCache();
            }
        }
        System.out.println("缓存预热成功");
    }
}
